!function (factory) {
    "use strict";
    "function" === typeof define && define.amd ? define(["jquery"], factory) : factory(jQuery);
}(function ($) {

    var HtmlAjax = function (options) {

        // 初始化请求参数
        this.defaults = {
            url: '/',
            type: 'GET',
            data: null,
            async: true,
            cache: true
        };
        this.options = $.extend({}, this.defaults, options || {});
    };

    HtmlAjax.prototype = {
        ajax: function (params) {
            // 执行请求
            var $element = $(this);
            $element.ace_ajax('startLoading');
            $.ajax(params).error(function () {
                $element.ace_ajax('stopLoading', true);
            }).done(function (resultContent) {
                $element.ace_ajax('stopLoading', true);
                resultContent = String(resultContent)
                    .replace(/<(title|link)([\s\>])/gi, '<div class="hidden ajax-append-$1"$2')
                    .replace(/<\/(title|link)\>/gi, "</div>");
                $element.empty().html(resultContent);
                setTimeout(function () {
                    var $head = $("head");
                    // $head.find("link.ace-ajax-stylesheet").remove();
                    for (
                        var b = ["link.ace-main-stylesheet", "link#main-ace-style", 'link[href*="/ace.min.css"]', 'link[href*="/ace.css"]']
                            , c = []
                            , d = 0;
                        d < b.length && (c = $head.find(b[d]).first(), !(c.length > 0));
                        d++
                    ) ;
                    // 已加载的link
                    var $hrefAry = $head.find("link.ace-ajax-stylesheet");
                    $element.find(".ajax-append-link").each(function (b) {
                        var d = $(this);
                        // 避免重复加载
                        var exists = false;
                        $hrefAry.each(function (index, item) {
                            if (item.href.indexOf(d.attr("href")) !== -1) {
                                exists = true;
                            }
                        });
                        if (d.attr
                            ("href") && !exists) {
                            var e = jQuery("<link />", {
                                type: "text/css",
                                rel: "stylesheet",
                                "class": "ace-ajax-stylesheet"
                            });
                            c.length > 0 ? e.insertBefore(c) : e.appendTo("head");
                            e.attr("href", d.attr("href"))
                        }
                        d.remove()
                    });
                }, 10);
                $("html,body").animate({scrollTop: 0}, 250);
            })
        }
    };

    $.fn.xumfHtmlAjax = $.fn.xumf_html_ajax = function (options) {

        return this.each(function () {
            var htmlAjax = new HtmlAjax(options);
            htmlAjax.ajax.call(this, htmlAjax.options);
        });
    }
});